package com.sam.questionBank;

/**
 * 标号1-n的n个人首尾相接，1到3报数，报到3的退出，求最后一个人的标号
 */
public class NumberOff {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            System.out.println(numberOff3(i));
        }
//        System.out.println(numberOff3(10000));
    }

    public static int numberOff3(int n) {
        boolean[] out = new boolean[n];
        int outCount = 0;
        int index = 0;
        int callNum = 1;

        while (true) {
            if (!out[index]) {
                if (outCount == n - 1) {
                    return index + 1;
                }
                if (callNum == 3) {
                    callNum = 0;//后面会++
                    out[index] = true;
                    outCount++;
                }
                callNum++;
            }
            index++;
            if (index == n) {
                index = 0;
            }
        }

    }
}
